package cn.jee.service;

import cn.jee.cache.impl.RedisCache;
import cn.jee.dao.StuDao;
import cn.jee.domain.Stu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class StuService {
  @Autowired
  StuDao dao;
  @Autowired
  RedisCache cache;
  public List<Stu> findAll(){
    if(cache.exists("stu:all")){
      //有缓存，从redis中返回
      log.info("缓存命中");
      Optional<List<Stu>> res=cache.get("stu:all");
      if(res.isEmpty())return List.of();
      else return  res.get();
    }
    else{
      //无缓存，查数据库，放入缓存，返回
      log.info("没有命中");
      var res =dao.findAll();
      cache.put("stu:all",res);
      return res;
    }
  }
  public void update(){
    cache.clear("stu");
    //update
  }

}
